﻿// -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	SPEnvironment.cs
//  Developer:	Igor Solomatov
//  Created:	12.11.2012
// 
// -------------------- iSharePoint -------------------- //

using System;
using System.ComponentModel;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;

namespace Smart.Common
{
	[Localizable(false)]
	public static class SPEnvironment
	{
		private static int _isInSandbox = -1;
		private static object _lockObj;

		static SPEnvironment()
		{
			Reset();
		}

		/// <summary>
		/// Resets the SharePoint environment, clearing all cached information.
		/// </summary>
		public static void Reset()
		{
			_isInSandbox = -1;
			_lockObj = new object();
		}

		/// <summary>
		/// Gets the friendly application domain name for the current application domain.
		/// </summary>
		/// <returns>The friendly name of the current application domain</returns>
		public static string GetDomainFriendlyName()
		{
			return AppDomain.CurrentDomain.FriendlyName;
		}

		/// <summary>
		/// Returns true if running in the sandbox, false otherwise.
		/// </summary>
		public static bool InSandbox
		{
			[SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
			[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
			get
			{
				if (_isInSandbox == -1)
				{
					lock (_lockObj)
					{
						if (_isInSandbox == -1)
						{
							_isInSandbox = GetDomainFriendlyName().Contains("Sandbox") ? 1 : 0;
						}
					}
				}

				return _isInSandbox == 1;
			}
		}
	}
}